/**
 * @(#) MedicalMoneyProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	건강보험증 Proxy - Proxy
 * 				건강보험증를 조회를한다.
 */
package com.skt.common_hr.ethics.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.utilSilver.textSilver;
	import com.skt.common_hr.ethics.model.vo.mainListVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
 
	public class EthicsProxy extends CommonProxy
	{
		public static const NAME:String = "BankreqProxy";
		
		public var mainList:ArrayCollection;
		public var listRead:ArrayCollection;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function EthicsProxy(data:Object=null)
		{
			super(NAME, data);
		}

		// 메인리스트 호출
		public function loadList(setFunc:Function, BEGDA:Date, ENDDA:Date):void
		{
			var pattern:RegExp = /-/g;
			var params:Object = new Object();
			
			params["I_PERNR"] = Variables.myPernr;
			params["I_BEGDA"] = String(DateUtil.toDefaultString(BEGDA).replace(pattern, ''));
			params["I_ENDDA"] = String(DateUtil.toDefaultString(ENDDA).replace(pattern, ''));
			
			loadData("Z_HR_EC06", params, loadListResult, listFault, setFunc);
		}
		
		public function readList(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = Variables.myPernr;
			
			loadData("Z_HR_EC06", params, readListResult, listFault, setFunc);
		}
		
		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0) {							
				if(event.result.OTAB is ArrayCollection) {
					trace('OTAB  ok');
				}else{
					trace('OTAB  false');
					event.result.OTAB = new ArrayCollection(event.result.OTAB);
				}
				mainList 		= parseMainList(event.result.OTAB);
				var rtnObj:Object =  {};
				rtnObj.mainList 			= mainList;
				
				event.result.func(rtnObj);
			}
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}
		
		public function readListResult(event:ResultEvent):void
		{
			if(retCode==0) {							
				if(event.result.OTAB is ArrayCollection) {
					trace('OTAB  ok');
				}else{
					trace('OTAB  false');
					event.result.OTAB = new ArrayCollection(event.result.OTAB);
				}
				listRead		= parseMainList(event.result.OTAB);
				var rtnObj:Object =  {};
				rtnObj.readList 			= listRead;
				
				event.result.func(rtnObj);
			}
		}

		private function readListFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}
		
		public function savePrint(vo:mainListVO, setFunc:Function):void
		{
			var pattern:RegExp = /-/g;
			
			var tables:Object = new Object();
			var obj:Object = {};
			obj.I_PERNR = Variables.myPernr;
			obj.I_BEGDA =  sToKorString(vo.BEGDA);
			obj.I_ENDDA = sToKorString(vo.ENDDA);

			var table:Array = new Array();			
			var obj2:Object = {};
			obj2.PYES = "X";
			table.push(obj2);
			
			tables["ITAB"] = table;

			saveData("Z_HR_EC07", obj, tables, savePrintResult, saveFault, setFunc);
		}

		public function savePrintResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}
			else {
				Alert.show(retText);
			}
		}
		
		private function saveFault(event:FaultEvent):void
		{
			Alert.show('WorkContractProxy.as listFault err: ' + retText);
		}

		private static var dateFormatter:DateFormatter = new DateFormatter();
		public static function toKorString(src:Date):String
		{
			dateFormatter.formatString = "YYYY년 MM월 DD일";
			return dateFormatter.format(src);
		}
		
		public static function sToKorString(src:String):String
		{
			dateFormatter.formatString = "YYYY-MM-DD";
			return dateFormatter.format(src);
		}

		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var rec:Object in recArr) 
			{
				var vo:mainListVO = new mainListVO();
				
				vo.ADDR			= rec["ADDR"];
				vo.BEGDA		= textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["BEGDA"]))	
				vo.CDATE		= textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["CDATE"]));
				vo.CYES			= rec["CYES"];
				vo.ECODE		= rec["ECODE"];
				vo.ENAME		= rec["ENAME"];
				vo.ENDDA		= textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["ENDDA"]));
				vo.PERNR		= textSilver.pernrFunc(rec["PERNR"]);
				vo.PYES			= rec["PYES"];
				vo.REGNO		= textSilver.regnoFunc(rec["REGNO"]);
				vo.SEQNR		= rec["SEQNR"];
				vo.CTIME		= rec["CTIME"];
				vo.JOINT		= textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["JOINT"]));
				vo.CTIME		= rec["CTIME"];
				vo.ORGTX		= rec["ORGTX"];
				vo.ETEXT		= rec["ETEXT"];
				vo.CHASU   		= rec["CHASU"];
				
				if(vo.CYES == 'X' ) {
					vo.CDATE 	= DateUtil.toDefaultString(DateUtil.stringToDateTime(rec["CDATE"]) ) + ' ' + DateUtil.toTimeString(DateUtil.stringToDateTime(rec["CTIME"]));	
				} else {
					vo.CDATE 	= '';
				}						
				
				if(vo.CYES == "X") {
					vo.JOINT = textSilver.toDotSpaceString(DateUtil.stringToDateTime(rec["JOINT"]));
				}
				else {
					vo.JOINT = ""; 
				}
				
				vo.CTIME		= DateUtil.toTimeString(DateUtil.stringToDateTime(rec["CTIME"]));
				vo.PDATE  		= DateUtil.toDefaultString(DateUtil.stringToDateTime(rec["PDATE"]) );
				vo.BEGDA_F		= toKorString(DateUtil.stringToDateTime(rec["CBEGDA"]) );
				vo.ENDDA_F		= toKorString(DateUtil.stringToDateTime(rec["CENDDA"]) );				
				vo.CDATE_F		= toKorString(DateUtil.stringToDateTime(rec.CDATE) );
				
										
				if(vo.CYES == "X") {
					vo.CYES_F = "V";
				}
				else {
					vo.CYES_F = "";
				}
				
				if(vo.PYES == "X") {
					vo.PYES_F = "V";
				}
				else {
					vo.PYES_F = "";
				}
				
				retArr.addItem(vo);
			}
			return retArr;
		}
	}
}